// pages/dynamic/dynamic.js
var api = require("../../config/api.js")
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value:'',
        hot_articleList: [],
        recommend:{},
        active: 0,
        activeName: "",
    },

    onChange2(event) {
        this.setData({
          activeName: event.detail,
        });
      },
    onChange(e) {
        if(e.detail)
        this.setData({
          value: e.detail,
        });
      },
      onSearch(e) {

          if (this.data.value){
            wx.showToast({
              title: '搜索中: ' + this.data.value,
              icon: 'loading',
            })
              wx.request({
                url: api.DynamicAPI,
                data: {
                    value:this.data.value
                },
                dataType: 'json',
                method: 'post',
                responseType: 'text',
                success: (result) => {
                    console.log(result)
                    this.setData({
                        hot_articleList:result.data
                    })
                },
                fail: (res) => {},
                complete: (res) => {},
              })
          }
      },
      onClick() {
        // Toast('搜索' + this.data.value);
      },
    SearchRecommendation:function(){
        console.log('1')
        // url: 'http://127.0.0.1:8000/api/dynamic/',
        wx.request({
            url: api.DynamicAPI,
            data: '',
            dataType: 'json',
            method: 'get',
            responseType: 'text',
            success: (result) => {
                console.log(result.data)
                this.setData({
                    recommend:result.data
                })
            },
            fail: (res) => {},
            complete: (res) => {},
          })
    },
      // 查看文章详情
  ViewDetails:function(e){
    var articleId = e.target.dataset.id
    console.log(e.target.dataset.id)
    wx.navigateTo({
      url: "../../pages/articledetail/articledetail?"+'articleId='+articleId,
    })
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log('onLoad', app.globalData.value)
        if (app.globalData.value){
            var value = app.globalData.value
            console.log('dy',value)
            this.setData({
                value:value
            })
            console.log(app.globalData.value)
            this.onSearch()
        }

        // this.SearchRecommendation()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // console.log('asasa')
        this.SearchRecommendation()
        // this.onSearch()
        this.onLoad()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})